<?php
   session_start();
   $cart_arr=isset($_SESSION["cart"])?$_SESSION["cart"]:'';
  /* var_dump($cart_arr);
   die();*/
   if($_SERVER['REQUEST_METHOD'] == 'POST'){

       $flag=true;
       if($_POST['user_name']=='' || $_POST['eamil']=="" || $_POST['address']=="" || $_POST['tel']=="" || $_POST['card']=="" || $_POST['date']==""){
           echo "Please input parameters";
           $flag=false;
       }


        if($flag && !preg_match('/^[a-zA-Z ]+$/',$_POST['user_name'])) {
              echo "Name Formatting error";
              $flag=false;
       }

       if($flag && !preg_match(' /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/',$_POST['eamil'])) {
           echo "Email Formatting error";
           $flag=false;
       }

       if($flag && !preg_match('/^[a-zA-Z0-9 ]+$/',$_POST['address'])) {
           echo "Address Formatting error";
           $flag=false;
       }

       if($flag && !preg_match('/^((\+614)|(04))\d{6,8}$/',$_POST['tel'])) {
           echo "Mobile Phone Formatting error";
           $flag=false;
       }

       if($flag && !preg_match('/^[0-9 ]{12,19}$/',$_POST['card'])) {
           echo "Credit Card Phone Formatting error";
           $flag=false;
       }

       if($flag && !preg_match('/^(\d{2})\/(\d{2})\/(\d{4})$/',$_POST['date'])) {
           echo "Date Phone Formatting error";
           $flag=false;
       }
       if($flag){
           $_SESSION["user"]=$_POST;
           header("location:receipt.php");
       }
   }

   require_once('tools.php');
   top_module('Check out');
?>
<?php
if(!empty($cart_arr)){


    $table_header=<<<"HTML"
         <table style="border-collapse: collapse;margin-bottom: 20px;">
          <tr>   
            <th style="border: 1px solid #aaa;padding: 10px 15px;">ID</th>
            <th style="border: 1px solid #aaa;padding: 10px 15px;">productName</th>
            <th style="border: 1px solid #aaa;padding: 10px 15px;">Option</th>
            <th style="border: 1px solid #aaa;padding: 10px 15px;">Quantity</th>
            <th style="border: 1px solid #aaa;padding: 10px 15px;">Price</th>
         </tr>
HTML;

    echo $table_header;
    $sum_price=0;
    foreach($cart_arr as $k=>$arr){
        $price = $arr['price'];
        $quantity = $arr['quantity'];
        $id = $arr['id'];
        $name = $arr['name'];
        $option= $arr['option'];
        $picture= $arr['picture'];
        $total_price=$price*$quantity;
        $sum_price+=$total_price;
        $arr_html= <<<"HTML"

         <tr>
           <td style="border: 1px solid #aaa;padding: 10px 15px;">$id</td> 
           <td style="border: 1px solid #aaa;padding: 10px 15px;">$name</td> 
           <td style="border: 1px solid #aaa;padding: 10px 15px;">$option</td> 
           <td style="border: 1px solid #aaa;padding: 10px 15px;">$quantity</td> 
           <td style="border: 1px solid #aaa;padding: 10px 15px;">$%1.2f</td> 
         </tr> 
  
HTML;

        printf($arr_html,$total_price);;
    }

    $sub_html=<<<"HTML"
       <tr>
           <td colspan="5" style="border: 1px solid #aaa;padding: 10px 15px;text-align: right;">Total Price：$%1.2f</td> 
         </tr> 
HTML;

    printf($sub_html,$sum_price);
    echo " </table>";

}


$form_html= <<<"HTML"
       
       
        <form action="" method="POST">
            <fieldset>
               <div style="padding-bottom: 10px;">
                 Name : <input type="text" name="user_name" id="user_name" />
                 <span id="error_user_name" style="color: red;"></span>
               </div>
                <div style="padding-bottom: 10px;">
                 Email : <input type="text" name="eamil" id="eamil" />
                 <span id="error_eamil" style="color: red;"></span>
                </div>
                <div style="padding-bottom: 10px;">
                 Address : <input type="text" name="address" id="address" />
                 <span id="error_address" style="color: red;"></span>
                </div>
                <div style="padding-bottom: 10px;">
                 Mobile Phone : <input type="text" name="tel" id="tel" />
                 <span id="error_tel" style="color: red;"></span>
                </div>
                <div style="padding-bottom: 10px;">
                 Credit Card: <input type="text" name="card" id="card" oninput="check_card()" />
                 <span id="error_card" style="color: red;"></span>
                 <img id="visa" src="./pictures/visa.jpg" style="height: 22px;vertical-align:middle;display: none;">
                </div>
                 <div style="padding-bottom: 10px;">
                 Expiry Date: <input type="text" name="date" id="date" />
                 <span id="error_date" style="color: red;"></span>
                </div>
                <input type="submit" value="submit" />
            </fieldset>
        </form>
HTML;



$html = <<<"HTML"
      <script>
          /* function validateForm() {
              var user_name=document.getElementById("user_name").value;
              var eamil=document.getElementById("eamil").value;
              var address=document.getElementById("address").value;
              var tel=document.getElementById("tel").value;
              var card=document.getElementById("card").value;
              var date=document.getElementById("date").value;
            
              if(user_name=='' || eamil=="" || address=="" || tel=="" || card=="" || date==""){
                  alert("Please input parameters");
                  return false;
              }
              var name_re = new RegExp("^[a-zA-Z ]+$"); 
              if (!name_re.test(user_name)) {
                  document.getElementById("error_user_name").innerHTML ="Formatting error";
                  return false;
              }
              
              var email_re = /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
              if (!email_re.test(eamil)) {
                  document.getElementById("error_eamil").innerHTML ="Formatting error";
                  return false;
              }
              
              var address_re = new RegExp("^[a-zA-Z0-9 ]+$"); 
              if (!address_re.test(address)) {
                  document.getElementById("error_address").innerHTML ="Formatting error";
                  return false;
              }

              var tel_re = /^((\+614)|(04))\d{6,8}$/;
              if (!tel_re.test(tel)) {
                  document.getElementById("error_tel").innerHTML ="Formatting error";
                  return false;
              }
              var card_re = /^[0-9 ]{12,19}$/;
              if (!card_re.test(card)) {
                  document.getElementById("error_card").innerHTML ="Formatting error";
                  return false;
              }
              

              var date_re = /^(\d{2})\/(\d{2})\/(\d{4})$/;
              if (!date_re.test(date)) {
                  document.getElementById("error_date").innerHTML ="Formatting error";
                  return false;
              }
              
           } */
        
           function check_card() {
              var card=document.getElementById("card").value;
              var visa_re=/^4\d{12,15}$/;
              if (visa_re.test(card)) {
                       document.getElementById("visa").style.display ="inline-block";
              }else{
                     document.getElementById("visa").style.display ="none";
              }
           }
           
       </script>
    <html>
       <link id='stylecss' type="text/css" rel="stylesheet" href="css/singleProduct.css">

</html>
HTML;

echo $form_html;
end_module();
echo $html;


?>